package flc.ast.fragment;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.AppUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.apkextract.lib.model.AeUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.FileTransferActivity;
import flc.ast.adapter.AppAdapter;
import flc.ast.bean.ApkBean;
import flc.ast.bean.SelectFileBean;
import flc.ast.databinding.FragmentAppBinding;
import flc.ast.utils.SendFileManager;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.utils.RxUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AppFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AppFragment extends BaseNoModelFragment<FragmentAppBinding> {

    private SendFileManager mInstance;
    private AppAdapter appAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_app;
    }

    @Override
    protected void initView() {
        mInstance = SendFileManager.getInstance();
        mDataBinding.rvAppList.setLayoutManager(new LinearLayoutManager(mContext));
        appAdapter = new AppAdapter();
        appAdapter.setEdit(true);
        mDataBinding.rvAppList.setAdapter(appAdapter);
        appAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        appAdapter.getItem(position).setSelected(!appAdapter.getItem(position).isSelected());
        appAdapter.notifyDataSetChanged();
        boolean hasExist = false;
        int tmpPosition = -1;
        for (SelectFileBean videoCollectionBean : mInstance.getSelectFileBeans()) {
            tmpPosition++;
            if (appAdapter.getItem(position).pkgName.equals(videoCollectionBean.getPath())) {
                hasExist = true;
                break;
            }
        }
        if (hasExist) {
            mInstance.deleteSelectFile(tmpPosition);
        } else {
            SelectFileBean bean = new SelectFileBean();
            bean.setType(4);
            bean.setName(AppUtils.getAppName(appAdapter.getItem(position).pkgName));
            bean.setPath(appAdapter.getItem(position).pkgName);
            AppUtils.AppInfo appInfo;
            appInfo = AppUtils.getAppInfo(appAdapter.getItem(position).pkgName);
            File apkFile = new File(appInfo.getPackagePath());
            bean.setSize(apkFile.length());
            mInstance.addSelectFile(bean);
        }
        ((FileTransferActivity) mContext).refreshDataCount(mInstance);
    }

    @Override
    protected void initData() {
        loadApks();
    }

    private void loadApks() {
        showDialog(getString(com.stark.common.res.R.string.loading));
        RxUtil.create(new RxUtil.Callback<List<String>>() {
            @Override
            public void doBackground(ObservableEmitter<List<String>> emitter) {
                emitter.onNext(AeUtil.getNoSystemInstalledPackageNames());
            }

            @Override
            public void accept(List<String> packageNames) {
                dismissDialog(500L);
                if (packageNames != null && packageNames.size() > 0) {
                    List<ApkBean> apkBeans = new ArrayList<>();
                    ApkBean bean;
                    for (String packageName : packageNames) {
                        bean = new ApkBean();
                        bean.pkgName = packageName;
                        apkBeans.add(bean);
                    }
                    mDataBinding.rvAppList.setVisibility(View.VISIBLE);
                    mDataBinding.tvAppNotData.setVisibility(View.GONE);
                    appAdapter.setList(apkBeans);
                } else {
                    mDataBinding.rvAppList.setVisibility(View.GONE);
                    mDataBinding.tvAppNotData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

}